package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DReturnDao {
    public static long dReturnDao(String sName){
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            connection = DBConnection.getConnection();
            preparedStatement = connection.prepareStatement("select * from discs where d_name=?");
            preparedStatement.setString(1, sName);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()&&resultSet.getInt(5)==0) {
                Date date =Date.valueOf( LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                resultSet.getInt(2);
                Date date1 = resultSet.getDate(3);
                long temp = (date.getTime()-date1.getTime())/1000/60/60/24*resultSet.getInt(2);
                preparedStatement=connection.prepareStatement("update discs set d_ZT=? where d_name=?");
                preparedStatement.setBoolean(1, true);
                preparedStatement.setString(2, sName);
                preparedStatement.executeUpdate();
                return temp;
            }
    }catch (Exception e){
        return -1;}finally {
            DBConnection.release(connection,preparedStatement,resultSet);
        }
        return -1;
    }
}
